﻿/* *************************************************
 *  Copyright © FlexDev 2010. All rights reserved.
 *  Author: Rodrigo da Silva Brito
 *  Date: 07/16/2010
 *  Contact: mailto:rodrigosilvabrito@hotmail.com
 *  Site: http://www.flexdev.com.br
 * *************************************************
*/
using System;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Flexdev.Security;

namespace Flexdev.Wcf.Extensions
{
    [AttributeUsage(AttributeTargets.Class)]
    public class FlexdevBehavior : Attribute, IEndpointBehavior, IServiceBehavior
    {
        #region IEndpointBehavior Members

        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            string userName = string.Empty;
            string password = string.Empty;
            
            foreach (IEndpointBehavior behavior in endpoint.Behaviors)
            {
                ClientCredentials clientCredentials = behavior as ClientCredentials;
                if (clientCredentials != null)
                {
                    userName = CryptService.Encrypt(clientCredentials.UserName.UserName, FlexdevHeader.PasswordKey, AesCryptographyLevel.AES_256);
                    password = CryptService.Encrypt(clientCredentials.UserName.Password, FlexdevHeader.PasswordKey, AesCryptographyLevel.AES_256);
                    break;
                }
            }

            FlexdevMessageInspector inspector = new FlexdevMessageInspector(userName, password);
            clientRuntime.MessageInspectors.Add(inspector);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        #endregion

        #region IServiceBehavior Members

        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
            {
                ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;
                if (channelDispatcher != null)
                {
                    foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
                    {
                        FlexdevMessageInspector inspector = new FlexdevMessageInspector();
                        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
                    }
                }
            }
        }

        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
        }

        #endregion
    }
}
